package com.dbManage.tools;

import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

@Component
public class CodeTool {
	public static BufferedImage getImage(String number) {
		try {
			BufferedImage image = new BufferedImage(100, 34,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = image.getGraphics();
			Random r = new Random();
			g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
			g.fillRect(0, 0, 100, 34);
			g.setColor(new Color(0, 0, 0));
			g.setFont(new Font(null, Font.BOLD, 22));
			g.drawString(number, 5, 25);
			for (int i = 0; i < 8; i++) {
				g.setColor(new Color(r.nextInt(255), r.nextInt(255), r
						.nextInt(255), r.nextInt(255)));
				g.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100),
						r.nextInt(30));
			}
			return image;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getNumber(int size) {
		String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String number = "";
		Random r = new Random();
		for (int i = 0; i < size; i++) {
			number += str.charAt(r.nextInt(str.length()));
		}
		return number;
	}
}
